﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;

namespace jQueryDemo.BusinessObject.Entity
{
    public abstract class BusinessObjectBase
    {
        #region Helper classes
        /// <summary>
        /// This attribute is to add an "ignored" flag on an attribute
        /// so the custom data-reflection binding methods (namely LoadRow)
        /// will not process the attribute
        /// </summary>
        [AttributeUsage(AttributeTargets.Property)]
        public class ReflectionIgnored : System.Attribute { }
        #endregion

        #region Class properties
        public int Id { get; set; }
        public bool Active { get; set; }
        #endregion

        #region Class methods
        public abstract void Load();
        public abstract void Insert();
        public abstract void Update();
        public abstract void Delete();
        /// <summary>
        /// Populate current instance properties with the data from a data row
        /// </summary>
        /// <param name="row"></param>
        protected void LoadRow(DataRow row)
        {
            //Get the list of all properties
            PropertyInfo[] props = GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            //Then loop and populate with data row info
            foreach (PropertyInfo prop in props)
            {
                //Only process properties that are not ReflectionIgnored()
                if (!prop.IsDefined(typeof(ReflectionIgnored), true) && !row[prop.Name].Equals(DBNull.Value))
                    prop.SetValue(this, row[prop.Name], null);
            }
        }
        #endregion
    }
}
